package com.jas.common;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

//@author A0121310J
public class Log {
        //Log status (level)
        public static final int SEVERE = -2;
        public static final int WARNING = -1;
        public static final int INFO = 0;
        public static final int FINE = 1;
        public static final int FINEST = 2;
       
        //Logging API
        //
        //follow the java.util.logging
        //Log handler - ConsoleHandler,FileHandler,MemoryHandler,SocketHandler,StreamHandler
        //Formatter - SimpleFormatte,XMLFormatter
        //Level of logging - SEVERE, WARNING, INFO, FINE ... etc
       
       
        private static Logger logger;
        private static FileHandler fh;
        private static boolean isInit = false;
        private static ArrayList<String> buffer;
       
       
        //Usage of addLog function:
        //Log.addLog("123456 added");
        public static void addLog(String msg){
                if(!isInit){
                        initLog();
                        isInit = true;
                }
               
                logger.log(Level.INFO, msg);
       
        }
       
        //Add log with varies type
       
        //Usage of addLog function (with other type)
        //Log.addLog("123456 added",Log.INFO);
        public static void addLog(String msg,int type){
                if(!isInit){
                        initLog();
                        isInit = true;
                }
                switch(type){
                case SEVERE:
                        logger.log(Level.SEVERE,msg);break;
                       
                case WARNING:
                        logger.log(Level.WARNING,msg);break;
                       
                case INFO:
                        logger.log(Level.INFO,msg);break;
               
                case FINE:
                        logger.log(Level.FINE,msg);break;
               
                case FINEST:
                        logger.log(Level.FINEST,msg);break;
               
                }
        }
       
        //Log.clearLog();
        public static void clearLog(){
                ArrayList<String> clearLog = new ArrayList<String>();
                writeBufferToLog(clearLog);
        }
       
       
       
       
       
        //helper functions
                private static void saveLogToBuffer(){
                        File f = new File("JAS.log");
                        buffer = readFile(f);
                }
                private static void saveLogToBuffer(ArrayList<String> buf){
                        File f = new File("JAS.log");
                        buffer = readFile(f,buf);
                       
                }
                private static void writeBufferToLog(){
                        File f = new File(Utilities.getUserHomeDirectory()+File.separator+"JAS.log");
                        writeFile(f,buffer);
                }
                private static void writeBufferToLog(ArrayList<String> buf){
                        File f = new File(Utilities.getUserHomeDirectory()+File.separator+"JAS.log");
                        writeFile(f,buf);
                }
               
                private static void initLog(){
                        try{
                                fh = new FileHandler(Utilities.getUserHomeDirectory()+File.separator+"JAS.log", true);
                         } catch (IOException e) {
                                 e.printStackTrace();
                         }
                       
                         logger = Logger.getLogger(Log.class.getName());
                         fh.setFormatter(new SimpleFormatter());
                         logger.addHandler(fh);
                         logger.setUseParentHandlers(false);
                         logger.setLevel(Level.CONFIG);
                         
                       
                }
               
       
        //helper functions for File I/O
        public static ArrayList<String> readFile(File f){
                ArrayList<String> fileLineBuffer = new ArrayList<String>();
                try{
                        //read lines and temp stored
                        FileReader fr = new FileReader(f);
                        BufferedReader br = new BufferedReader(fr);
                        String line = br.readLine();
                        while(line != null){
                                fileLineBuffer.add(line);
                                line = br.readLine();
                        }
               
                        fr.close();
                        br.close();
                }catch(IOException ioe){
                        ioe.printStackTrace();
                }catch(Exception e){
                        e.printStackTrace();
                }
                return fileLineBuffer;
        }
        public static ArrayList<String> readFile(File f,ArrayList<String> buffer){
                ArrayList<String> fileLineBuffer = buffer;
                try{
                        //read lines and temp stored
                        FileReader fr = new FileReader(f);
                        BufferedReader br = new BufferedReader(fr);
                        String line = br.readLine();
                        while(line != null){
                                fileLineBuffer.add(line);
                                line = br.readLine();
                        }
               
                        fr.close();
                        br.close();
                }catch(IOException ioe){
                        ioe.printStackTrace();
                }catch(Exception e){
                        e.printStackTrace();
                }
                return fileLineBuffer;
        }
       
        public static String writeFile(File f,ArrayList<String> lineBuffer){
                String returnStr = "";
               
                try{
                        FileWriter fw = new FileWriter(f);
                        BufferedWriter bw = new BufferedWriter(fw);
                       
                        for(String s:lineBuffer){
                                bw.write(s+'\n');
                        }
                               
                               
                       
                        bw.flush();
                       
                        fw.close();
                        bw.close();
                       
                }catch(IOException ioe){
                        ioe.printStackTrace();
                }catch(Exception e){
                        e.printStackTrace();
                }
                return returnStr;
        }

       
}
